package com.dyzs.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.dyzs.activity.money.MoneyMainActivity;
import com.dyzs.activity.plan.PlanListActivity;
import com.dyzs.activity.resume.AddResumeActivity;
import com.dyzs.activity.tool.ToolMainActivity;
import com.dyzs.activity.word.WordMainActivity;
import com.dyzs.util.Constants;
import com.dyzs.util.DatabaseHelper;
import com.dyzs.util.ToolUtils;

public class MainActivity extends CommonActivity {
	
	public  int[] menu_image_array = {R.drawable.xcap,R.drawable.tyxx,R.drawable.dtdh,R.drawable.skb,R.drawable.bbx,R.drawable.dyc,R.drawable.lxsj,R.drawable.cszn,	R.drawable.dybk,};

	private GridView gv;	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main); 
        setTitle("首页");
        gv=(GridView)findViewById(R.id.main_gv);   
        setGV();
        createDB();
    }
    
    
    private void setGV(){  	
    	gv.setAdapter(getMenuAdapter(getResources().getStringArray(R.array.main_menu),menu_image_array));    
    	gv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {	
				boolean title=true;
				Intent intent = null;								
				Constants.isComput=true;
				switch (arg2) {				
				case 6://记账
					if(!Constants.LOGIN_NONET){
						title=false;
						intent = new Intent(MainActivity.this,MoneyMainActivity.class);
					}
					break;
				case 5://导游词
					if(!Constants.LOGIN_NONET){
						title=false;
						intent = new Intent(MainActivity.this,WordMainActivity.class);
					}
					break;				
				case 4://工具箱
					if(!Constants.LOGIN_NONET){						
						title=false;
						intent = new Intent(MainActivity.this,ToolMainActivity.class);
					}
					break;	
				case 0://行程信息
					title=false;
					intent = new Intent(MainActivity.this,PlanListActivity.class);
					intent.putExtra("fromType", "planType");
					break;
				case 1://团员信息
					title=false;
					intent = new Intent(MainActivity.this,PlanListActivity.class);
					intent.putExtra("fromType", "memberType");
					break;
				case 7://求职
					title=false;
					intent = new Intent(MainActivity.this,AddResumeActivity.class);					
					break;			
				}
				if(arg2==3){//航班
					if(ToolUtils.checkBrowser(MainActivity.this,"com.flight.view")){						
						startActivity(ToolUtils.starBrowser("com.flight.view","com.flight.view.Main"));										   
					}else{						
						showDialog(MainActivity.this,"航班管家","flightmanager3.0.apk");				
					}				
				}
				else if(arg2==8){//导游博客
					if(ToolUtils.checkBrowser(MainActivity.this,"com.tencent.WBlog")){						
						startActivity(ToolUtils.starBrowser("com.tencent.WBlog","com.tencent.WBlog.WBlogFirstRun"));						
					}else{						
						showDialog(MainActivity.this,"导游微博","QQweibo.apk");							
					}
				}
				//地图
				else if(arg2==2){
					if(ToolUtils.checkBrowser(MainActivity.this,"com.google.android.apps.maps")){						
						startActivity(ToolUtils.starBrowser("com.google.android.apps.maps","com.google.android.maps.MapsActivity"));					   
					}else{
						showDialog(MainActivity.this,"谷歌地图","Google_maps_5.1.0.apk");				
					}				
				}
				else if(title){
					Toast.makeText(MainActivity.this, R.string.no_net,Toast.LENGTH_SHORT).show();
				}else{
				
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}				
			}
		});	
	}
    
    
    
    
	
    
    
   
    
	  private void createDB(){
		  DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);		 
		  SQLiteDatabase db = dbHelper.getReadableDatabase();	
		  db.close();
	  }
    
    
	private SimpleAdapter getMenuAdapter(String[] menuNameArray,int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,	R.layout.main_menu_item, 
				new String[] { "itemImage",	"itemText" }, new int[] { R.id.item_image,R.id.item_text });
		return simperAdapter;
	}
	
		
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//			backClient();
			android.os.Process.killProcess(android.os.Process.myPid()); 
//        	
			return true;
		}
		return super.onKeyDown(keyCode,event);
	}
	 
	
		public boolean onCreateOptionsMenu(Menu menu) {
		   super.onCreateOptionsMenu(menu);
		   menu.add(0, 0, 0, "退出系统");		  
		   return true;
		}
		
		@Override  
	    public boolean onOptionsItemSelected(MenuItem item) {   
	        super.onOptionsItemSelected(item);   
	        switch (item.getItemId()) {   
	            case 0:  
	            	android.os.Process.killProcess(android.os.Process.myPid()); 
//	            	backClient();  
	                break; 	           
	        }   
	        return true;   
	    }
		
		
		

		
		
}